//
//  MCMyOrderVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import MJRefresh

class MCMyOrderVc: QMUICommonTableViewController {
    
    let MCMyOrderIdentifier:String = "MCMyOrderIdentifier"
    
    var count = 1
    
    var dataSource:Array = [MCRecordsList?]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "订单记录"
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = UIColor.qmui_color(withHexString: "F7F7F9")
        self.tableView.register(MCMyOrderTableCell.self, forCellReuseIdentifier: MCMyOrderIdentifier)
        
        
        self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.count = 1
            self.myOrder(count: self.count)
            
        })
        
        self.tableView.mj_header?.beginRefreshing()
        
        self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.myOrder(count: self.count)
        })
        
        
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 84
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MCMyOrderTableCell = tableView.dequeueReusableCell(withIdentifier: MCMyOrderIdentifier) as! MCMyOrderTableCell
        
        let recordListInfo = self.dataSource[indexPath.row]
        
        if recordListInfo?.orderType == 1 {
            cell.titleVipLabel.text = "连续包年"
        }else if recordListInfo?.orderType == 2 {
            cell.titleVipLabel.text = "12个月会员"
        }else if recordListInfo?.orderType == 3 {
            cell.titleVipLabel.text = "连续包月"
        } else  {
            cell.titleVipLabel.text = "1个月会员"
        }
        
        if recordListInfo?.orderStatus == 0 {
            cell.statusInfoLabel.text = "待支付"
        }else if recordListInfo?.orderStatus == 1 {
            cell.statusInfoLabel.text = "已支付"
        }else if recordListInfo?.orderStatus == 2{
            cell.statusInfoLabel.text = "已取消"
        }else if recordListInfo?.orderStatus == 3 {
            cell.statusInfoLabel.text = "已退款"
        }else {
            cell.statusInfoLabel.text = "未知状态"
        }
        
        
        cell.dateLabel.text = recordListInfo?.payTime ?? ""
        
        if recordListInfo?.orderPrice == nil {
            cell.moneyLabel.text = "¥0"
        }else{
            cell.moneyLabel.text = "¥\(String(describing: recordListInfo?.orderPrice!))"
        }
        
       
        
        
        return cell
    }
    
    
    func myOrder(count:Int?) -> Void {
        NetWorkRequest(MCMyOrderApi.myOrderList(current:count)) { ResponseModel in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if count == 1 {
                self.dataSource.removeAll()
                
                let orderModel = MCMyOrderList(JSONString: ResponseModel.dataString)
                
                self.dataSource.append(contentsOf:orderModel?.recordsList ?? [])
                
                 
            }else {
                self.count += 1
                let orderModel = MCMyOrderList(JSONString: ResponseModel.dataString)
                
                self.dataSource.append(contentsOf: orderModel?.recordsList ?? [])
                
            }
            
            self.tableView.reloadData()
            
        } failureCallback: { ResponseModel in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
    }
    
    
}
